#!/usr/bin/python
# -*- coding:utf-8 -*-
# @Time  : 6/6/20
# @Author: yanlh
# @usage : 设置日志级别为info
import docker_utils


def get_plugin_info():
    plugin_info = {
        "name": "Docker_dockerd_001 --log-level",
        "plugin_id": "Docker_dockerd_001",
        "plugin_type": "Docker_Dockerd_Config_Safe",
        "info": "Ensure the logging level is set to 'info'",
        "level": "B",
        "module": "Safety scan",
        "author": "yanlh",
        "keyword": "Safety scan",
        "configable": "false",
    }
    return plugin_info


logger = None
cur_user = None
cur_module = None
cur_task = None


def set_plugin_logger(setter, user, module, task, *args, **kwargs):
    global logger, cur_user, cur_module, cur_task
    logger = setter
    cur_user = user
    cur_module = module
    cur_task = task


def single_logger_record(level, des, module='_Scan'):
    if level == 'error':
        logger.debug_error(cur_user, cur_module, cur_task + module, '', des)
    if level == 'info':
        logger.debug_info(cur_user, cur_module, cur_task + module, '', des)
    if level == 'warning':
        logger.debug_warning(cur_user, cur_module, cur_task + module, '', des)


def parse_config(result):
    if '--log-level' in result and '--log-level=info' not in result:
        des = "Not Safe. Parameter --log-level should be default or 'info'."
        single_logger_record('warning', des)
        return [des], 1
    des = 'Safe. Parameter --log-level set safe.'
    single_logger_record('info', des)
    return [des], 0


# 扫描函数
def scan(func_args, flag=0):
    single_logger_record('info', "Scan Start.")
    mark, result = docker_utils.is_dockerd_exist(func_args)
    if not mark:
        single_logger_record('warning', result)
        return [result], 1
    scan_list, error_count = parse_config(';'.join(result))
    single_logger_record('info', 'Scan Complete.')
    if flag:
        info = reinforce()
        scan_list.extend(info)
        return scan_list, error_count
    return scan_list, error_count


def reinforce():
    info = [docker_utils.rein_info[0], docker_utils.rein_info[1]]
    single_logger_record('info', docker_utils.rein_info[0], '_Reinforce')
    single_logger_record('info', docker_utils.rein_info[1], '_Reinforce')
    return info


# 回滚函数
def rollback(func_args):
    des_list = [docker_utils.rollback_info[0], docker_utils.rollback_info[1]]
    single_logger_record('info', docker_utils.rollback_info[0], '_Rollback')
    single_logger_record('info', docker_utils.rollback_info[1], '_Rollback')
    return des_list, 0


def check(ip, *args, **kwargs):
    sys_user = kwargs.get("system_user")
    sys_pwd = kwargs.get("system_pwd")
    comm = kwargs.get("command")
    try:
        single_logger_record('info', "docker_dockerd_001 Start.", '_Check')
        func_args = {'ip': ip, 'username': sys_user, 'passwd': sys_pwd, 'comm': comm}
        result = docker_utils.check_func(scan, rollback, func_args)
        single_logger_record('info', "docker_dockerd_001 Complete.", '_Check')
        return result
    except Exception as er:
        code = 1
        des = ["ERROR:", str(er)]
        single_logger_record('error', des, '_Check')
        return {"code": code, "count": 0, "des": des}


if __name__ == '__main__':
    """
    本地测试
    """
    check(ip=None, command=2)
